//
//  YJCycleImages.m
//  YJ-轮播图
//
//  Created by 冀永金 on 16/7/21.
//  Copyright © 2016年 冀永金. All rights reserved.
//

#import "YJCycleImages.h"

@interface YJCycleImages ()<UIScrollViewDelegate>

@property (nonatomic, strong) UIScrollView *cycleScrollView;
@property (nonatomic, strong) UIPageControl *page;
@end

@implementation YJCycleImages
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    if (self.cycleScrollView.contentOffset.x < self.bounds.size.width) {
        scrollView.contentOffset =  CGPointMake(self.bounds.size.width * 5, 0);
    }else if (self.cycleScrollView.contentOffset.x > 5 * self.bounds.size.width) {
        scrollView.contentOffset = CGPointMake(self.bounds.size.width, 0);
    }
    _page.currentPage = ( scrollView.contentOffset.x - self.bounds.size.width) / self.bounds.size.width;

}
#pragma mark ----初始化
- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        //初始化 不能出现self.
       // self.backgroundColor = [UIColor yellowColor];
        // 子控件初始化时，frame不要与初始化方法的frame一样
        //推荐使用 self.bounds / CGRectMake(0, 0, frame.size.width, frame.size.height)
        
        _cycleScrollView = [[UIScrollView alloc]initWithFrame:self.bounds];
        _cycleScrollView.delegate = self;
        //_cycleScrollView.backgroundColor = [UIColor blackColor];
        [self addSubview:_cycleScrollView];
        _page = [[UIPageControl alloc]initWithFrame:CGRectMake(0, 0, frame.size.width / 3, 20)];
        _page.center = CGPointMake(frame.size.width / 2, frame.size.height - 10 );
        self.page.numberOfPages = 5;
        [self addSubview:_page];
        [_page addTarget:self action:@selector(handleMove:) forControlEvents:UIControlEventValueChanged];
        
    }
    return self;
}
- (void)handleMove:(UIPageControl *)page {
    [self.cycleScrollView setContentOffset:CGPointMake(self.bounds.size.width * (page.currentPage + 1), 0) animated:YES];
    
}

- (void)setImageWithArray:(NSArray *)array {
    //判断类型（不知道别人使用你的类型的时候传进来的是什么类型）
    id firstObject = [array firstObject];
    NSMutableArray *imagesArray = [NSMutableArray array];
    if ([firstObject isKindOfClass:[NSString class]]) {
        // 把字符串 数组 转成UIImage 数组
        for (NSString *imageName in array) {
            UIImage *image = [UIImage imageNamed:imageName];
            [imagesArray addObject:image];
        }
        
    }else if ([firstObject isKindOfClass:[UIImage class]]) {
        [imagesArray setArray:array];
    }
    UIImage *firstImage = [imagesArray firstObject];

    UIImage *lastImage = [imagesArray lastObject];
    [imagesArray insertObject:lastImage atIndex:0];
    [imagesArray addObject:firstImage];
    for (int i = 0; i < imagesArray.count; i ++) {
        UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(self.bounds.size.width * i, 0, self.bounds.size.width, self.bounds.size.height)];
        imageView.image = imagesArray[i];
        [self.cycleScrollView addSubview:imageView];
       
        
    }
    NSLog(@"%ld",imagesArray.count);
    _cycleScrollView.contentSize = CGSizeMake(imagesArray.count * self.bounds.size.width, 0);
    _cycleScrollView.pagingEnabled = YES;
    _cycleScrollView.bounces = YES;
    _cycleScrollView.showsHorizontalScrollIndicator = NO;
    _page.numberOfPages = array.count;
    _cycleScrollView.contentOffset = CGPointMake(self.bounds.size.width, 0);
}



/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
